var timeDataMap = {
    'year': [1,2,3,4,5,6,7,8,9,10,11,12],
    'month': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
    'day': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],
    'hour': [0,2,3,4,5,6,7,8,9,10,
        11,12,13,14,15,16,17,18,19,20,
        21,22,23,24,25,26,27,28,29,30,
        31,32,33,34,35,36,37,38,39,40,
        41,42,43,44,45,46,47,48,49,50,
        51,52,53,54,55,56,57,58,59]
};
var subLevelDevMap = {
    713 :{
        ele : 'E001',
        gas : 'G001'
    },
    714 :{
        ele : 'E009'
    },
    715 :{
        ele : 'E070'
    },
    716 :{
        ele : 'E276'
    },
    717 :{
        ele : 'E037',
        gas : 'G059'
    },
    718 :{
        ele : 'E401',
        gas : 'G002'
    }

}
var timeFormatMap = {
    'year': "%Y-%m",
    'month': "%Y-%m-%d",
    'day': "%Y-%m-%d %H",
    'hour': "%Y-%m-%d %H:%m"
};
var timeSingleFormatMap = {
    'year': "%m",
    'month': "%d",
    'day': "%H",
    'hour': "%m"
};

dev.action = function(resp,param){
    var theTime = param.time;
    var workSpace = param.workSpace;
    var db = iot.db();
    var format,singleFormate;
    var electricityResult = null;
    var gasResult = null;
    // 2019之后的数据
    var carbonEq2019Result = null;

    var timeSub = parseInt(theTime.substring(0,4));

    var resultData = {
        xData : iot.list(),
        yLeft: iot.list(),
        yRight : iot.list()
    };

    var timeType = iot.getTimeByFormat(theTime);
    var timeDataList = timeDataMap[timeType];
    format = timeFormatMap[timeType];
    singleFormate = timeSingleFormatMap[timeType];

    if(timeSub >= 2019){
        var fuSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
            " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
            " SUM(dev_win.dvalue) totalSum" +
            " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 2 and name = '碳排' and energy_struc_id = " + workSpace + ") esp" +
            " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
            " and dev_win.the_time like '" + theTime + "%' group by time";

        carbonEq2019Result = db.qry(fuSql);

    }else{
        //工厂  E001._用量*0.734 +  G001.用量*2.355
        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].ele){
            var doSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
                " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
                " SUM(dev_win.dvalue) totalSum" +
                " FROM iot_dev_data_win dev_win " +
                " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
                " where 1=1 " +
                " and dev.sn='" + subLevelDevMap[workSpace].ele + "'" +
                " and dev_win.data_type = '_用量'" +
                " and dev_win.the_time like '" + theTime + "%' group by time";

            electricityResult = db.qry(doSql);
        }

        if(subLevelDevMap[workSpace] && subLevelDevMap[workSpace].gas){
            gasResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
                " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
                " SUM(dev_win.dvalue) totalSum" +
                " FROM iot_dev_data_win dev_win " +
                " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
                " where 1=1 " +
                " and dev.sn='"+subLevelDevMap[workSpace].gas+"'" +
                " and dev_win.data_type = '用量'" +
                " and dev_win.the_time like '"+ theTime +"%' group by time");
        }

    }

    // 产量
    var yieldResult = db.qry("select DATE_FORMAT(the_time,'" + format + "') time," +
        " DATE_FORMAT(the_time,'" + singleFormate + "') timeIx," +
        " SUM(dvalue) totalSum from iot_dev_data_win" +
        " where dev_id = 278 and the_time like '"+ theTime +"%' group by time");

    var dm = iot.getNumberFormat("00");

    if(timeSub >= 2019){

        for(var ix in timeDataList){
            var timeItem = dm.format(timeDataList[ix]);
            var data  = 0;
            carbonEq2019Result.forAll(function (item,ix) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    data = parseInt(item.totalSum);
                }
            })
            resultData.xData.add(timeItem);
            resultData.yLeft.add(data);
        }

    }else{

        for(var ix in timeDataList){
            var timeItem = dm.format(timeDataList[ix]);
            var data  = 0;
            electricityResult.forAll(function (item,ix) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    data = item.totalSum*0.734;
                }
            })
            gasResult.forAll(function (item,ix) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    data = parseInt(data + (item.totalSum * 2.355));
                    // data += (item.totalSum*2.355);
                }
            })
            resultData.xData.add(timeItem);
            resultData.yLeft.add(data);
        }

    }

    for(var ix in timeDataList){
        var timeItem = dm.format(timeDataList[ix]);
        var data  = 0;
        yieldResult.forAll(function (item,ix) {
            if(item && item.timeIx && item.timeIx === timeItem){
                data = item.totalSum;
            }
        })
        resultData.yRight.add(data);
    }
    resp.setJsData(resultData);
}